package org.yy4ever.smartremind;

import java.io.InputStream;

import android.app.ListActivity;
import android.content.ContentUris;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class ConcactList extends ListActivity {
	
	Cursor cursor;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);
        
        cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null,null, null);
		startManagingCursor(cursor);
		
		String[] from = new String[]{ContactsContract.Data.DISPLAY_NAME};
		int[] to = new int[]{android.R.id.text1};
		CursorAdapter adapter = new SimpleCursorAdapter(this,
				android.R.layout.simple_list_item_single_choice,cursor,from,to);
		
        setListAdapter(adapter);
    }

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		//super.onListItemClick(l, v, position, id);
		cursor.moveToPosition(position);
		
		int index_id = cursor.getColumnIndex(ContactsContract.Data._ID);
		int index_name = cursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME);
		int index_photo = cursor.getColumnIndex(ContactsContract.Data.PHOTO_ID);
		int _id = cursor.getInt(index_id);
		String name = cursor.getString(index_name);
		index_photo = cursor.getInt(index_photo);
		
		Intent intent = this.getIntent();
		Bundle bundle = new Bundle();
		bundle.putInt("id", _id);
		bundle.putString("name", name);
		bundle.putInt("photo", index_photo);
		
		intent.putExtras(bundle);
		setResult(RESULT_OK, intent);
		finish();
	}
    
    
}